package com.yazi.mxz.person.controller.firm;

import java.util.List;
import java.util.Set;

import javax.annotation.Resource;


import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.myframework.smhj.common.exception.MyException;
import com.myframework.smhj.jackson.result.StatusResult;
import com.yazi.mxz.common.entity.beans.CompanyMember;
import com.yazi.mxz.common.entity.beans.CompanyMemberId;
import com.yazi.mxz.common.entity.beans.Member;
import com.yazi.mxz.common.service.CompanyMemberService;
import com.yazi.mxz.common.service.CompanyService;
import com.yazi.mxz.common.service.MemberService;

@Controller("firm-companyMemberController")
@RequestMapping("/firm/companyMember")
public class CompanyMemberController extends BaseController {
	private static Logger log = LoggerFactory.getLogger(CompanyMemberController.class);
	
	@Resource
	MemberService memberService;
	@Resource
	CompanyMemberService companyMemberService;
	@Resource
	CompanyService companyService;
	
	@RequestMapping("/list")
	public String list(Model model) {
		debug(log, "进入企业管理员列表页面");
		
		Set<CompanyMember> companyMembers = getCompany(true).getCompanyMembers();
		
		model.addAttribute("companyMembers", companyMembers);
		model.addAttribute("cmId", getMemberId());
		return "firm/companyMember_list";
	}
	
	@RequestMapping("/add")
	public String add() {
		debug(log, "进入企业管理员添加页面");
		return "firm/companyMember_add";
	}
	
	@RequestMapping("/addDo/json")
	@ResponseBody
	public StatusResult addDoJson(String userName) {
		debug(log, "添加企业管理员");
		StatusResult jsonResult = new StatusResult();
		try {
			Member member = new Member();
			member.setName(userName);
			List<Member> mList = memberService.list(member);
			if (mList.size() != 1) {
				jsonResult.setErrorMessage("用户不存在");
				return jsonResult;
			}
			member = mList.get(0);
			
			CompanyMemberId cmId = new CompanyMemberId();
			cmId.setMemberId(member.getMemberId());
			cmId.setCompanyId(getCompanyId());
			
			CompanyMember cm = new CompanyMember();
			cm.setId(cmId);
			
			companyMemberService.add(cm);
			jsonResult.setOkMessage("添加成功");
		} catch (MyException e) {
			debug(log, "添加企业管理员失败：" + e.getMessage());
			jsonResult.setFailMessage(e.getMessage());
		} catch (Exception e) {
			error(log, e, "添加企业管理员异常");
			jsonResult.setErrorMessage("添加失败，请联系管理员");
		}
		return jsonResult;
	}
	
	@RequestMapping("/delDo/json")
	@ResponseBody
	public StatusResult delDoJson(long memberId) {
		debug(log, "删除企业管理员");
		StatusResult jsonResult = new StatusResult();
		try {
			companyMemberService.remove(getCompanyId(), memberId);
			jsonResult.setOk();
		} catch (Exception e) {
			error(log, e, "删除企业管理员异常");
			jsonResult.setErrorMessage("删除失败，请联系管理员");
		}
		return jsonResult;
	}
}
